<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    //成功返回
    public function success($data='', $msg = "success"): JsonResponse
    {
        $result = [
            "code"  => 0,
            "msg"   => $msg,
            "data"  => $data,
        ];
        return response()->json($result);
    }

    // 成功返回 数据表格
    public function table($data, $msg = "success"): JsonResponse
    {
        if(method_exists($data,'total')){
            $result = [
                "code"  => 0,
                "msg"   => $msg,
                "count" => $data->total(),
                "data"  => $data->items(),
            ];
        }else{
            $result = [
                "code"  => 0,
                "msg"   => $msg,
                "data"  => $data,
            ];
        }

        return response()->json($result);
    }

    //失败返回
    public function failed($msg = "failed"): JsonResponse
    {
        $result = [
            "code"  => -1,
            "msg"   => $msg,
            "data"  => ''
        ];
        return response()->json($result);
    }
}
